package com.smart.config.constant;

import java.util.Arrays;
import java.util.List;

/**
 * @Author xiaoqiang
 * @Version RedisConstant.java, v 0.1 2025年03月17日 18:01 xiaoqiang
 * @Description: TODO
 */
public class RedisConstant {

    public static final String REDIS_KEY_PREFIX = "smart_config_";

    private static final String REDIS_HISTORY_KEY_PREFIX = "smart_config_history";


    private static final String VERSION_PREFIX = "version";

    public static final String REDIS_KEY_SPLIT = ":";


    public static final String REDIS_KEY_VERSION_SPLIT = "#";


    public static final String REDIS_CHANNEL_PREFIX = "smart_config_channel";


    public static final Integer MIN_PULL_INTERVAL = 3;

    public static final String REDIS_KEY_PREFIX_FORMAT = "{%s}";

    /**
     * 构建监听redis消息的频道名称
     *
     * @param appName
     * @return
     */
    public static final String buildRedisChannelName(String appName) {
        return String.format(REDIS_KEY_PREFIX_FORMAT, REDIS_KEY_PREFIX + appName) + REDIS_KEY_SPLIT + REDIS_CHANNEL_PREFIX;
    }

    public static final String buildVersionKey(String appName, String group, String dataId) {
        return buildRedisKey(appName, group, dataId) + REDIS_KEY_SPLIT + VERSION_PREFIX;
    }

    /**
     * 构建发布消息format,供lua脚本使用
     * @param key
     * @param versionFormat
     * @return
     */
    public static final String buildPubMessageFormat(String key, String versionFormat) {
        return key + REDIS_KEY_VERSION_SPLIT + versionFormat;
    }

    public static final String buildPubMessage(String key, Long version) {
        return key + REDIS_KEY_VERSION_SPLIT + version;
    }

    public static final String[] splitPubMessage(String message) {
        return message.split(REDIS_KEY_VERSION_SPLIT);
    }

    /**
     * 构建redis key
     *
     * @param appName
     * @param group
     * @param dataId
     * @return
     */
    public static final String buildRedisKey(String appName, String group, String dataId) {
        return String.format(REDIS_KEY_PREFIX_FORMAT, REDIS_KEY_PREFIX + appName) + REDIS_KEY_SPLIT + group + REDIS_KEY_SPLIT + dataId;
    }

    /**
     * 构建redis history key
     *
     * @param appName
     * @param group
     * @param dataId
     * @return
     */
    public static final String buildHistoryKey(String appName, String group, String dataId) {
        return buildRedisKey(appName, group, dataId) + REDIS_KEY_SPLIT + REDIS_HISTORY_KEY_PREFIX;
    }

    /**
     * 构建redis history receive key
     *
     * @param key
     * @param version
     * @return
     */
    public static final String buildHistoryKeyReceive(String key, Long version) {
        return key + REDIS_KEY_SPLIT + version;
    }

    /**
     * 构建redis history receive key
     *
     * @param appName
     * @param group
     * @param dataId
     * @param version
     * @return
     */
    public static final String buildHistoryKeyReceiveKey(String appName, String group, String dataId, Long version) {
        return buildRedisKey(appName, group, dataId) + REDIS_KEY_SPLIT + version;
    }
}
